1
|
|
|
export function hslaString(hslcolor) { |
2
|
|
|
if (hslcolor.a !== undefined) { |
3
|
|
|
return ( |
4
|
|
|
"hsla(" + |
5
|
|
|
hslcolor.h + |
6
|
|
|
"," + |
7
|
|
|
hslcolor.s + |
8
|
|
|
"%," + |
9
|
|
|
hslcolor.l + |
10
|
|
|
"%," + |
11
|
|
|
parseFloat(hslcolor.a, 10) + |
12
|
|
|
")" |
13
|
|
|
); |
14
|
|
|
} |
15
|
|
|
return "hsl(" + hslcolor.h + "," + hslcolor.s + "%," + hslcolor.l + "%)"; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
export function rgbaString(hexcolor) { |
19
|
|
|
if (hexcolor.a !== undefined) { |
20
|
|
|
return ( |
21
|
|
|
"rgba(" + |
22
|
|
|
hexcolor.r + |
23
|
|
|
"," + |
24
|
|
|
hexcolor.g + |
25
|
|
|
"," + |
26
|
|
|
hexcolor.b + |
27
|
|
|
"," + |
28
|
|
|
parseFloat(hexcolor.a, 10) + |
29
|
|
|
")" |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
return "rgb(" + hexcolor.r + "," + hexcolor.g + "," + hexcolor.b + ")"; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
export function parseHalf(foo) { |
36
|
|
|
return parseInt(foo / 2, 10); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
export function compact(array) { |
40
|
|
|
let index = -1, |
41
|
|
|
length = array ? array.length : 0, |
42
|
|
|
resIndex = 0, |
43
|
|
|
result = []; |
44
|
|
|
|
45
|
|
|
while (++index < length) { |
46
|
|
|
let value = array[index]; |
47
|
|
|
if (value) { |
48
|
|
|
result[resIndex++] = value; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return result; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
export function omit(obj, fn) { |
55
|
|
|
var target = {}; |
56
|
|
|
for (var i in obj) { |
|
|
|
|
57
|
|
|
if (fn(i)) { |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
if (!Object.prototype.hasOwnProperty.call(obj, i)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
target[i] = obj[i]; |
64
|
|
|
} |
65
|
|
|
return target; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
export function serializeOptions(options) { |
69
|
|
|
if (typeof options !== "object") { |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
var cleanOptions = omit(options, function(prop) { |
73
|
|
|
return prop.indexOf("gm_") === 0; |
74
|
|
|
}), |
75
|
|
|
sortedOpts = Object.entries(cleanOptions) |
76
|
|
|
.filter(function(item) { |
77
|
|
|
return ( |
78
|
|
|
typeof item[1] !== "function" && |
79
|
|
|
typeof item[1] !== "object" && |
80
|
|
|
item[1] !== null && |
81
|
|
|
typeof item[1] !== "undefined" |
82
|
|
|
); |
83
|
|
|
}) |
84
|
|
|
.sort(); |
85
|
|
|
return JSON.stringify(sortedOpts); |
86
|
|
|
} |
87
|
|
|
|
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: